package Class20240812.Dao;

import java.sql.*;
import java.util.SortedMap;

public class DeptDao {
    public int insertDept(int deptno, String dname, String loc) {
        int result;
        try {
            String url = "jdbc:mysql://127.0.0.1:3306/a65?serverTimezone=UTC";
            String username = "root";
            String password = "415253";
            Class.forName("jdbc:mysql://127.0.0.1:3306/a65?serverTimezone=UTC");
            Connection connection = DriverManager.getConnection(url, username, password);
            String sql = "";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, deptno);
            preparedStatement.setString(2, dname);
            preparedStatement.setString(3, loc);
            result = preparedStatement.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public ResultSet selectDept() {
        try {
            String url = "jdbc:mysql://127.0.0.1:3306/a65?serverTimezone=UTC";
            String username = "root";
            String password = "415253";
            Class.forName("jdbc:mysql://127.0.0.1:3306/a65?serverTimezone=UTC");
            Connection connection = DriverManager.getConnection(url, username, password);
            String sql = "";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs=preparedStatement.executeQuery();
            while(rs.next())
            {
                int deptno=rs.getInt("deptno");
                String dname=rs.getString("dname");
                String loc= rs.getString("loc");
                System.out.println(deptno+"\t"+dname+"\t"+loc);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
